import { getParkingLotReuqest, searchParkingLotRequest } from "@/apis/parking";
import { message } from "antd";
import { makeAutoObservable } from "mobx"
import { makePersistable } from "mobx-persist-store";

export interface OptionType {
	value: string | number;
	label: string | number;
	children?: OptionType[];
}

export interface parkingType {
	id: number,
	building: string,
	storey: number,
	x: number,
	y: number,
	status: number,
	type: number
	picture: string
	position: string
	pid: number
}

export interface cascaderOption {
	value: string | number;
	label?: React.ReactNode;
	disabled?: boolean;
	children?: cascaderOption[];
	// 标记是否为叶子节点，设置了 `loadData` 时有效
	// 设为 `false` 时会强制标记为父节点，即使当前节点没有 children，也会显示展开图标
	isLeaf?: boolean;
}

export class parkingLotStore {
	position: [number, number, number] | undefined = undefined
	status: 0 | 1 | undefined = undefined
	types: number | undefined = undefined
	parking: parkingType[] = []
	parking1: parkingType[] = []
	options: cascaderOption[] = []

	constructor() {
		makeAutoObservable(this)
		makePersistable(this, {
			name: 'parkingLotStore',
			properties: ['position', 'status', 'types', 'parking', 'parking1'],
			storage: window.localStorage,
		})
	}

	setPosition = (p: [number, number, number] | undefined) => {
		this.position = p
	}

	setStatus = (s: 0 | 1 | undefined) => {
		this.status = s
	}

	setTypes = (t: number) => {
		this.types = t
	}

	setParking = (parking: parkingType[]) => {
		this.parking = parking
	}

	setParking1 = (parking: parkingType[]) => {
		this.parking1 = parking
	}

	//查询
	searchBy = () => {
		searchParkingLotRequest({
			storey: this.position ? this.position[0] : undefined,
			x: this.position ? this.position[1] : undefined,
			y: this.position ? this.position[2] : undefined,
			status: this.status,
			type: this.types ? this.types : undefined
		}).then((res: any) => {
			message.success(res.message)
			this.setParking1(res.result)
		})
	}

	getParkingLot = (status: 0 | 1 | -1 = -1) => {
		getParkingLotReuqest().then((res: any) => {
			this.setParking(res.result)
			this.setParking1(res.result)
			this.setOptions(res.result, status)
		})
	}

	//生成cascader的options
	//参数：parking1：车位信息（一维数组），status：变为对应状态的options，diff：指定的车位可以不用disable
	setOptions = (parking1: parkingType[], status: 0 | 1 | -1 = -1, diff?: number) => {
		const re: cascaderOption[] = []
		parking1.forEach(item1 => {
			const index1 = re.findIndex(item2 => item2.label === item1.storey)
			if (index1 === -1) {
				re.push({
					label: item1.storey,
					value: item1.storey,
					children: [{
						label: item1.x,
						value: item1.x,
						children: [{
							label: item1.y,
							value: item1.id,
							disabled: (status !== -1 && item1.status !== status && item1.id !== diff)
						}]
					}]
				})
			} else {
				const index2 = re[index1].children?.findIndex(item2 => item2.label === item1.x)
				if (index2 === -1) {
					re[index1].children?.push({
						label: item1.x,
						value: item1.x,
						children: [{
							label: item1.y,
							value: item1.id,
							disabled: (status !== -1 && item1.status !== status && item1.id !== diff)
						}]
					})
				} else {
					(re[index1].children as cascaderOption[])[index2 as number].children?.push({
						label: item1.y,
						value: item1.id,
						disabled: (status !== -1 && item1.status !== status && item1.id !== diff)
					})
				}
			}
		})
		this.options = re
	}
}